
// 获取url中传过来的数据
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

// 添加购物车按钮
function local(data,index,num){
        let frg = false
        for (var i = 0; i < localStorage.length; i++) {
            if (index == localStorage.key(i)) {
                frg = true
            }
        }
        if (frg == true) {
            let zancun = JSON.parse(localStorage[index])
            // 数量+1
        //     console.log(zancun);
            zancun.report_number  = parseInt(zancun.report_number) + num
            // 再将数据封装到local里面
            localStorage.setItem(index, JSON.stringify(zancun));

        } else if (frg == false) {
            localStorage.setItem(index, JSON.stringify(data));
        }
        console.log(JSON.parse(localStorage[index]));
}


// 各种nav移入显示
function hoverXianshi(dom) {
        $(dom).on("mouseenter", function () {
            $(dom).find('div').css('display', 'block')
        })
        $(dom).on("mouseleave", function () {
            $(dom).find('div').css('display', 'none')
        })
    }
//渲染二级菜单
function bingNav(){
    $.ajax({
        url:'/list/bindnav.php',
        dataType:'json',
        success:function(res){
            let {list} = res
            let arr = []
            list.forEach( e=>{
                if(!arr.includes(e.tager)){
                    arr.push(e.tager)
                }
            } )
            var str = template('tmp_nav', {
                list: list,
                arr:arr
            })
            $('.hide_nav').html(str)

        }   
    })
}
// 移入显示二级菜单
function hide_nav(dom) {
    $(dom).on("mouseenter", function () {
        $('.hide_nav').stop().slideDown()
    })
    $(dom).on("mouseleave", function () {
        $('.hide_nav').stop().slideUp()
    })
}


// 去购物车结算链接
function goCart(){
        // 去购物车页面结算按钮
    $('.shopping-cart').on('click',function(e){
        e = e || window.event
        let target = e.target
        // 判斷如果没登录 ,去登录页面
        if(!username){
            alert('您还没有登录,请登录')
            window.location.href = 'http://www.yjf.com:8080/page/login.html'
        }else{
            window.location.href = 'http://www.yjf.com:8080/page/cart.html?name='+username
            if(target.className == 'go_cart'){
                window.location.href = 'http://www.yjf.com:8080/page/cart.html?name='+username
            }
        }     
    })
}

// 去详情页 传入参数为 元素类名 和 list 数据 只能li 用
function goDetails(ele,list){
// 点击li 将信息存到session 跳转页面
    $(ele).on('click','li',function(e){
        e.stopPropagation()
        let index =  $(this).index()
        window.sessionStorage.setItem(index,JSON.stringify(list[index]))
        // 携带index跳转页面 在详情页通过传过去的index获取对应的数据
        window.location.href = "http://www.yjf.com:8080/page/details.html?name="+username +"&index="+index
    })
}
    


// 點擊加入购物车 创造一个小元素 移动到购物车
function donghua(x,y){
    console.log(y)
    let span = document.createElement('span')
    $(span).addClass('ydSpan')
    $(span).css({
        'width':'20px',
        'height': '20px',
        'border-radius': '50px',
        'background-color': '#eb155b',
        'position': 'absolute',
        'top': y,
        'left':x,
        'z-index':'200',
    })
    const ele  = document.querySelector('body')
     ele.appendChild(span)
     $('.ydSpan').stop().animate({
         top: y - 100 +'px',
         opacity:0
     },
     500,
     function(){
         $('.ydSpan').remove()
     })

}